{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {
    "pycharm": {}
   },
   "source": [
    "## JSSP Genetic Algorithm Example\n",
    "\n",
    "This notebook demonstrates how to use the genetic algorithm."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {
    "pycharm": {}
   },
   "outputs": [],
   "source": [
    "from JSSP import CSVData\n",
    "\n",
    "# initialize data\n",
    "data_directory = '../data/given_data'\n",
    "instance_data = CSVData(data_directory + '/sequenceDependencyMatrix.csv',\n",
    "                              data_directory + '/machineRunSpeed.csv',\n",
    "                              data_directory + '/jobTasks.csv')\n",
    "\n",
    "# uncomment this to print the data that was read in\n",
    "# print(instance_data)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [],
   "source": [
    "from JSSP.genetic_algorithm import GASelectionEnum\n",
    "\n",
    "output_dir = './example_output'\n",
    "\n",
    "# optional\n",
    "output_results = True\n",
    "auto_open = True\n",
    "benchmark = True\n",
    "verbose = True\n",
    "progress_bar = True\n",
    "\n",
    "# GA parameters\n",
    "runtime = 30 # in seconds\n",
    "iterations = 100\n",
    "population_size = 500\n",
    "selection_method = GASelectionEnum.FITNESS_PROPORTIONATE\n",
    "selection_size = 5\n",
    "mutation_probability = 0.1"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {
    "pycharm": {}
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Running benchmark of GA\n",
      "Parameters:\n",
      "stopping_condition = 30\n",
      "time_condition = True\n",
      "population_size = 500\n",
      "selection_method = _fitness_proportionate_selection\n",
      "mutation_probability = 0.1\n"
     ]
    },
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||| Time: 0:00:29\n"
     ]
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "\n",
      "Solution\n",
      "makespan = 5138.5938065484015\n",
      "machine_makespans = [4556.620122337875, 4867.331924973143, 5138.5938065484015, 4819.484424183219, 4876.721371974385, 4310.4883893468605, 4908.917307398373, 4506.323169894541]\n",
      "operation_list =\n",
      "[[31  0  0  0]\n",
      " [30  0  0  7]\n",
      " [24  0  0  0]\n",
      " [41  0  0  4]\n",
      " [17  0  0  6]\n",
      " [27  0  0  4]\n",
      " [43  0  0  5]\n",
      " [19  0  0  2]\n",
      " [ 1  0  0  3]\n",
      " [ 4  0  0  1]\n",
      " [23  0  0  5]\n",
      " [45  0  0  5]\n",
      " [32  1  0  0]\n",
      " [32  0  0  6]\n",
      " [19  1  1  6]\n",
      " [14  0  0  0]\n",
      " [26  0  0  4]\n",
      " [ 2  0  0  4]\n",
      " [37  0  0  3]\n",
      " [33  0  0  6]\n",
      " [44  0  0  0]\n",
      " [35  0  0  5]\n",
      " [35  1  1  7]\n",
      " [ 3  0  0  5]\n",
      " [23  1  1  7]\n",
      " [ 0  0  0  5]\n",
      " [ 9  0  0  1]\n",
      " [ 5  0  0  2]\n",
      " [47  0  0  7]\n",
      " [25  1  0  7]\n",
      " [ 0  1  1  4]\n",
      " [31  2  1  3]\n",
      " [14  1  0  6]\n",
      " [39  0  0  6]\n",
      " [36  0  0  5]\n",
      " [40  0  0  3]\n",
      " [16  0  0  4]\n",
      " [ 7  0  0  5]\n",
      " [30  2  1  6]\n",
      " [38  0  0  4]\n",
      " [28  0  0  7]\n",
      " [14  2  1  6]\n",
      " [36  1  0  3]\n",
      " [48  0  0  5]\n",
      " [22  0  0  7]\n",
      " [ 6  0  0  1]\n",
      " [20  0  0  7]\n",
      " [27  1  1  7]\n",
      " [10  0  0  0]\n",
      " [10  1  1  1]\n",
      " [29  0  0  2]\n",
      " [ 3  1  1  0]\n",
      " [44  1  0  3]\n",
      " [38  1  1  3]\n",
      " [28  1  0  1]\n",
      " [ 4  2  0  0]\n",
      " [38  2  2  2]\n",
      " [13  0  0  0]\n",
      " [44  2  1  6]\n",
      " [ 6  2  0  7]\n",
      " [46  0  0  4]\n",
      " [ 8  0  0  1]\n",
      " [20  2  0  7]\n",
      " [10  2  2  0]\n",
      " [12  0  0  7]\n",
      " [18  0  0  6]\n",
      " [18  2  0  0]\n",
      " [16  2  1  0]\n",
      " [23  2  1  7]\n",
      " [21  1  0  5]\n",
      " [16  1  1  7]\n",
      " [12  1  1  5]\n",
      " [ 5  3  1  0]\n",
      " [26  1  1  6]\n",
      " [43  2  1  4]\n",
      " [12  2  1  0]\n",
      " [47  1  1  7]\n",
      " [22  1  1  0]\n",
      " [36  2  1  5]\n",
      " [22  2  1  4]\n",
      " [29  1  0  1]\n",
      " [34  0  0  7]\n",
      " [25  0  0  4]\n",
      " [13  1  0  3]\n",
      " [10  3  2  2]\n",
      " [45  2  0  3]\n",
      " [45  1  0  1]\n",
      " [ 5  2  1  2]\n",
      " [ 0  3  2  7]\n",
      " [32  2  1  6]\n",
      " [36  3  2  7]\n",
      " [28  2  1  2]\n",
      " [19  2  1  4]\n",
      " [43  1  1  1]\n",
      " [34  2  0  5]\n",
      " [ 7  2  1  6]\n",
      " [11  0  0  1]\n",
      " [ 5  1  1  3]\n",
      " [ 7  1  1  0]\n",
      " [ 0  2  2  0]\n",
      " [18  1  0  7]\n",
      " [34  1  0  0]\n",
      " [ 9  2  0  2]\n",
      " [30  3  1  5]\n",
      " [ 6  1  0  3]\n",
      " [ 3  2  1  2]\n",
      " [35  2  2  5]\n",
      " [ 9  1  0  4]\n",
      " [37  2  1  3]\n",
      " [14  3  2  4]\n",
      " [15  1  0  5]\n",
      " [31  1  1  4]\n",
      " [24  1  0  3]\n",
      " [24  2  1  5]\n",
      " [41  2  1  7]\n",
      " [37  1  1  7]\n",
      " [30  1  1  0]\n",
      " [21  0  0  7]\n",
      " [30  4  1  6]\n",
      " [25  2  1  0]\n",
      " [42  0  0  3]\n",
      " [ 1  2  1  2]\n",
      " [44  3  2  6]\n",
      " [11  2  1  0]\n",
      " [37  3  1  4]\n",
      " [47  3  2  6]\n",
      " [48  1  1  7]\n",
      " [17  1  1  4]\n",
      " [20  1  0  3]\n",
      " [ 1  1  1  6]\n",
      " [25  3  1  4]\n",
      " [42  1  1  4]\n",
      " [48  2  2  1]\n",
      " [42  2  2  4]\n",
      " [35  3  2  6]\n",
      " [40  1  1  0]\n",
      " [28  3  2  0]\n",
      " [31  3  2  4]\n",
      " [ 5  4  2  2]\n",
      " [32  3  2  0]\n",
      " [28  4  3  1]\n",
      " [11  1  1  4]\n",
      " [41  1  1  4]\n",
      " [32  4  3  7]\n",
      " [24  3  1  7]\n",
      " [ 2  1  1  6]\n",
      " [19  3  2  5]\n",
      " [37  4  2  2]\n",
      " [32  5  4  3]\n",
      " [37  5  3  2]\n",
      " [40  2  2  5]\n",
      " [12  4  2  7]\n",
      " [40  4  3  0]\n",
      " [ 2  2  1  2]\n",
      " [ 2  3  1  2]\n",
      " [31  4  3  6]\n",
      " [33  1  1  0]\n",
      " [33  2  2  4]\n",
      " [17  2  1  0]\n",
      " [33  3  3  3]\n",
      " [40  3  3  1]\n",
      " [ 8  1  0  4]\n",
      " [39  1  0  7]\n",
      " [39  2  1  4]\n",
      " [ 8  2  1  6]\n",
      " [ 1  3  2  5]\n",
      " [14  4  3  6]\n",
      " [21  2  1  2]\n",
      " [49  0  0  3]\n",
      " [26  2  1  7]\n",
      " [15  0  0  5]\n",
      " [21  3  2  0]\n",
      " [12  3  2  5]\n",
      " [ 4  1  0  3]\n",
      " [44  4  3  7]\n",
      " [29  2  1  6]\n",
      " [47  2  2  4]\n",
      " [ 1  4  2  2]\n",
      " [ 4  3  1  4]\n",
      " [29  4  2  3]\n",
      " [12  5  3  4]\n",
      " [46  1  0  2]\n",
      " [15  2  1  5]\n",
      " [ 2  4  2  6]\n",
      " [ 7  3  2  2]\n",
      " [13  2  1  6]\n",
      " [46  2  1  5]\n",
      " [27  2  2  0]\n",
      " [35  4  3  1]\n",
      " [34  3  1  0]\n",
      " [41  4  2  3]\n",
      " [49  1  1  7]\n",
      " [15  3  1  0]\n",
      " [26  3  2  5]\n",
      " [14  5  4  2]\n",
      " [49  2  1  0]\n",
      " [21  4  3  6]\n",
      " [21  5  3  5]\n",
      " [29  3  2  3]\n",
      " [44  5  4  2]\n",
      " [ 3  3  2  1]\n",
      " [ 3  4  3  7]\n",
      " [ 3  5  3  2]\n",
      " [41  5  2  1]\n",
      " [33  4  4  7]\n",
      " [41  3  2  3]\n",
      " [48  3  3  0]\n",
      " [48  4  4  1]\n",
      " [ 6  3  1  7]\n",
      " [24  4  2  6]\n",
      " [19  4  2  7]\n",
      " [ 0  5  3  4]\n",
      " [ 0  4  3  6]\n",
      " [34  4  2  6]\n",
      " [14  6  5  1]\n",
      " [24  5  3  4]\n",
      " [24  6  4  4]\n",
      " [31  5  4  0]\n",
      " [31  6  5  2]\n",
      " [ 6  5  2  7]\n",
      " [ 6  4  2  6]\n",
      " [ 0  6  4  2]]\n",
      "opening file:///home/mcfadd/Projects/Job_Shop_Schedule_Problem/examples/example_output/example_benchmark in browser\n"
     ]
    }
   ],
   "source": [
    "from JSSP.solver import Solver\n",
    "\n",
    "# create solver\n",
    "solver = Solver(instance_data)\n",
    "\n",
    "# run genetic algorithm\n",
    "solution = solver.genetic_algorithm_time(runtime=runtime, \n",
    "                                         population_size=population_size,\n",
    "                                         selection_method_enum=selection_method,\n",
    "                                         selection_size=selection_size,\n",
    "                                         mutation_probability=mutation_probability, \n",
    "                                         benchmark=benchmark,\n",
    "                                         verbose=verbose,\n",
    "                                         progress_bar=progress_bar\n",
    "                                        )\n",
    "\n",
    "    \n",
    "print('\\nSolution')\n",
    "print(solution)\n",
    "\n",
    "if output_results:\n",
    "    if benchmark:\n",
    "        solver.output_benchmark_results(output_dir, name='example_benchmark', auto_open=auto_open)\n",
    "    else:\n",
    "        solution.create_schedule(output_dir)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "#### You can also run the genetic algorithm for a certain number of iteratins instead of time"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {
    "pycharm": {
     "name": "#%%\n"
    }
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Running benchmark of GA\n",
      "Parameters:\n",
      "stopping_condition = 100\n",
      "time_condition = False\n",
      "population_size = 500\n",
      "selection_method = _fitness_proportionate_selection\n",
      "mutation_probability = 0.1\n",
      "\n",
      "Solution\n",
      "makespan = 4561.86291000841\n",
      "machine_makespans = [4183.043453882814, 4561.86291000841, 4496.868421052631, 4522.201345924237, 4282.737175951461, 4162.2873684210535, 4439.347398418827, 4391.394586964099]\n",
      "operation_list =\n",
      "[[10  0  0  2]\n",
      " [37  0  0  2]\n",
      " [ 1  0  0  6]\n",
      " [40  0  0  3]\n",
      " [35  0  0  0]\n",
      " [12  0  0  7]\n",
      " [43  0  0  4]\n",
      " [22  0  0  6]\n",
      " [19  0  0  1]\n",
      " [10  1  1  5]\n",
      " [13  1  0  3]\n",
      " [28  0  0  7]\n",
      " [ 9  1  0  1]\n",
      " [15  0  0  5]\n",
      " [30  0  0  7]\n",
      " [20  0  0  0]\n",
      " [34  0  0  7]\n",
      " [26  0  0  4]\n",
      " [33  0  0  7]\n",
      " [ 3  0  0  0]\n",
      " [44  0  0  2]\n",
      " [30  2  1  1]\n",
      " [46  1  0  5]\n",
      " [44  1  0  3]\n",
      " [46  0  0  4]\n",
      " [ 6  0  0  6]\n",
      " [36  1  0  3]\n",
      " [ 2  0  0  4]\n",
      " [15  1  0  5]\n",
      " [39  1  0  7]\n",
      " [ 7  0  0  6]\n",
      " [25  1  0  7]\n",
      " [30  3  1  5]\n",
      " [30  4  1  2]\n",
      " [12  2  1  0]\n",
      " [30  1  1  7]\n",
      " [18  1  0  5]\n",
      " [38  0  0  7]\n",
      " [21  0  0  7]\n",
      " [18  2  0  0]\n",
      " [20  1  0  2]\n",
      " [ 5  0  0  2]\n",
      " [35  1  1  3]\n",
      " [31  0  0  0]\n",
      " [27  0  0  4]\n",
      " [39  0  0  6]\n",
      " [21  1  0  5]\n",
      " [13  0  0  4]\n",
      " [22  2  1  7]\n",
      " [29  0  0  2]\n",
      " [29  1  0  7]\n",
      " [22  1  1  0]\n",
      " [24  0  0  0]\n",
      " [32  0  0  2]\n",
      " [36  0  0  5]\n",
      " [ 8  0  0  1]\n",
      " [48  0  0  0]\n",
      " [42  0  0  3]\n",
      " [49  0  0  7]\n",
      " [14  1  0  6]\n",
      " [34  1  0  0]\n",
      " [ 9  2  0  4]\n",
      " [19  2  1  4]\n",
      " [28  1  0  5]\n",
      " [49  1  1  7]\n",
      " [16  0  0  5]\n",
      " [47  0  0  3]\n",
      " [ 8  1  0  4]\n",
      " [32  1  0  2]\n",
      " [40  1  1  1]\n",
      " [24  1  0  6]\n",
      " [23  0  0  3]\n",
      " [17  0  0  6]\n",
      " [34  2  0  1]\n",
      " [12  1  1  4]\n",
      " [45  0  0  5]\n",
      " [ 0  0  0  5]\n",
      " [ 6  1  0  1]\n",
      " [14  0  0  0]\n",
      " [25  0  0  0]\n",
      " [45  2  0  7]\n",
      " [ 0  1  1  0]\n",
      " [23  2  1  7]\n",
      " [47  1  1  7]\n",
      " [ 4  2  0  0]\n",
      " [ 4  0  0  1]\n",
      " [ 5  3  1  7]\n",
      " [ 3  2  1  4]\n",
      " [ 1  2  1  2]\n",
      " [ 2  3  1  2]\n",
      " [31  2  1  7]\n",
      " [ 2  1  1  4]\n",
      " [ 5  2  1  1]\n",
      " [26  1  1  6]\n",
      " [35  3  2  6]\n",
      " [28  2  1  2]\n",
      " [ 3  1  1  3]\n",
      " [ 9  0  0  3]\n",
      " [38  1  1  2]\n",
      " [41  0  0  4]\n",
      " [ 6  2  0  5]\n",
      " [27  1  1  3]\n",
      " [17  2  1  0]\n",
      " [38  2  2  2]\n",
      " [41  2  1  7]\n",
      " [24  2  1  4]\n",
      " [11  0  0  5]\n",
      " [43  2  1  1]\n",
      " [ 5  1  1  6]\n",
      " [33  1  1  0]\n",
      " [11  1  1  7]\n",
      " [35  2  2  6]\n",
      " [23  1  1  7]\n",
      " [31  1  1  4]\n",
      " [27  2  2  0]\n",
      " [33  2  2  2]\n",
      " [18  0  0  3]\n",
      " [26  2  1  5]\n",
      " [10  3  2  2]\n",
      " [48  1  1  7]\n",
      " [24  3  1  7]\n",
      " [16  2  1  0]\n",
      " [41  1  1  5]\n",
      " [49  2  1  7]\n",
      " [45  1  0  1]\n",
      " [44  2  1  6]\n",
      " [20  2  0  2]\n",
      " [ 1  1  1  6]\n",
      " [14  2  1  6]\n",
      " [46  2  1  5]\n",
      " [15  3  1  0]\n",
      " [47  2  2  4]\n",
      " [ 2  2  1  4]\n",
      " [21  2  1  4]\n",
      " [21  3  2  0]\n",
      " [39  2  1  4]\n",
      " [13  2  1  7]\n",
      " [15  2  1  6]\n",
      " [ 4  1  0  3]\n",
      " [10  2  2  6]\n",
      " [40  2  2  4]\n",
      " [48  2  2  1]\n",
      " [34  3  1  6]\n",
      " [40  4  3  0]\n",
      " [ 1  4  2  0]\n",
      " [48  3  3  0]\n",
      " [ 3  3  2  4]\n",
      " [36  2  1  5]\n",
      " [36  3  2  7]\n",
      " [40  3  3  3]\n",
      " [12  3  2  5]\n",
      " [31  3  2  4]\n",
      " [31  4  3  6]\n",
      " [21  5  3  5]\n",
      " [ 3  4  3  3]\n",
      " [21  4  3  4]\n",
      " [31  5  4  0]\n",
      " [31  6  5  1]\n",
      " [12  4  2  7]\n",
      " [48  4  4  1]\n",
      " [ 5  4  2  2]\n",
      " [12  5  3  7]\n",
      " [34  4  2  5]\n",
      " [ 7  1  1  0]\n",
      " [43  1  1  7]\n",
      " [29  2  1  6]\n",
      " [35  4  3  2]\n",
      " [ 6  3  1  7]\n",
      " [17  1  1  4]\n",
      " [37  1  1  3]\n",
      " [19  1  1  5]\n",
      " [19  3  2  5]\n",
      " [32  2  1  3]\n",
      " [ 8  2  1  2]\n",
      " [42  1  1  4]\n",
      " [42  2  2  5]\n",
      " [ 0  3  2  7]\n",
      " [ 0  2  2  0]\n",
      " [33  3  3  3]\n",
      " [16  1  1  7]\n",
      " [11  2  1  5]\n",
      " [26  3  2  1]\n",
      " [24  4  2  2]\n",
      " [37  2  1  7]\n",
      " [ 6  5  2  1]\n",
      " [47  3  2  2]\n",
      " [ 7  2  1  6]\n",
      " [33  4  4  7]\n",
      " [37  3  1  4]\n",
      " [44  3  2  6]\n",
      " [14  3  2  6]\n",
      " [14  4  3  4]\n",
      " [ 1  3  2  7]\n",
      " [ 4  3  1  5]\n",
      " [19  4  2  7]\n",
      " [ 2  4  2  6]\n",
      " [41  5  2  3]\n",
      " [41  3  2  3]\n",
      " [ 3  5  3  1]\n",
      " [ 0  4  3  0]\n",
      " [29  3  2  7]\n",
      " [37  4  2  2]\n",
      " [24  5  3  4]\n",
      " [24  6  4  4]\n",
      " [ 0  5  3  3]\n",
      " [41  4  2  3]\n",
      " [32  3  2  6]\n",
      " [14  5  4  0]\n",
      " [25  3  1  4]\n",
      " [14  6  5  7]\n",
      " [ 6  4  2  5]\n",
      " [ 7  3  2  2]\n",
      " [28  3  2  0]\n",
      " [25  2  1  2]\n",
      " [32  4  3  6]\n",
      " [32  5  4  6]\n",
      " [29  4  2  7]\n",
      " [ 0  6  4  6]\n",
      " [28  4  3  1]\n",
      " [37  5  3  3]\n",
      " [44  4  3  0]\n",
      " [44  5  4  0]]\n",
      "opening file:///home/mcfadd/Projects/Job_Shop_Schedule_Problem/examples/example_output/example_benchmark in browser\n"
     ]
    }
   ],
   "source": [
    "from JSSP.solver import Solver\n",
    "\n",
    "# create solver\n",
    "solver = Solver(instance_data)\n",
    "\n",
    "# run genetic algorithm\n",
    "solution = solver.genetic_algorithm_iter(iterations=iterations, \n",
    "                                         population_size=population_size,\n",
    "                                         selection_method_enum=selection_method,\n",
    "                                         selection_size=selection_size, \n",
    "                                         mutation_probability=mutation_probability, \n",
    "                                         benchmark=benchmark,\n",
    "                                         verbose=verbose\n",
    "                                        )\n",
    "\n",
    "print('\\nSolution')\n",
    "print(solution)\n",
    "\n",
    "if output_results:\n",
    "    if benchmark:\n",
    "        solver.output_benchmark_results(output_dir, name='example_benchmark', auto_open=auto_open)\n",
    "    else:\n",
    "        solution.create_schedule_xlsx_file(output_dir)\n"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.6.8"
  },
  "pycharm": {
   "stem_cell": {
    "cell_type": "raw",
    "metadata": {
     "collapsed": false
    },
    "source": []
   }
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
